package com.kayang.dd.controller;


import com.kayang.common.HttpMsg;
import com.kayang.dd.config.DDconfig;
import com.kayang.dd.service.AccessTokenService;
import com.kayang.dd.pojo.DDcontext;
import com.kayang.dd.pojo.DeptModel;
import com.kayang.dd.pojo.param.UpdateDeptParam;
import com.kayang.dd.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;


@RestController
@RequestMapping(value = "/dept")
public class DeptController extends BaseController {

    @Autowired
    AccessTokenService tokenService;

    @Autowired
    DDconfig dDconfig;

    @Autowired
    DeptService deptService;

    @PostMapping("/create")
    public ResponseEntity creatDept(@Valid @RequestBody DeptModel deptModel){
        String url = dDconfig.getPath() + "/department/create?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(deptModel.toMap(deptModel));
        Map ret = deptService.creatDept(deptModel, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("创建成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @PostMapping("/update")
    public ResponseEntity updateDept(@Valid @RequestBody UpdateDeptParam deptParam){
        String url = dDconfig.getPath() + "/department/update?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(deptParam.toMap(deptParam));
        Map ret = deptService.updateDept(deptParam, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("更新成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    // param {"dept_id": 100}
    @PostMapping("/delete")
    public ResponseEntity deleteDept(@Valid @RequestBody Map param){
        String url = dDconfig.getPath() + "/department/delete?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(param);
        Map ret = deptService.deleteDept(param, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("删除成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    /**
     * param:
     * {
     * 	"language":"zh_CN", //en_US
     * 	"dept_id":100
     * }
     * */
    @PostMapping("/get")
    public ResponseEntity getDept(@Valid @RequestBody Map param){
        String url = dDconfig.getPath() + "/department/get?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(param);
        Map ret = deptService.getDept(param, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("查看成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    /**
     * param:
     * {
     * 	"language":"zh_CN", //en_US
     * 	"dept_id":100 //父部门ID
     * }
     * */
    @PostMapping("/listsub")
    public ResponseEntity getSubDeptList(@Valid @RequestBody Map param){
        String url = dDconfig.getPath() + "/department/listsub?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(dDconfig.getAppKey()));
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(param);
        Map ret = deptService.getDept(param, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("查看成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }


}